home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / mailsnarf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  6.3 KB  |  269 lines

  1. /*
  2.   dsniff.c
  3.  
  4.   sniff mail on a network, saving messages in Berkeley mbox format
  5.   (for processing with mail -f, pine, mutt, whatever).
  6.   this is for demonstration purposes and educational use only.
  7.  
  8.   TODO: Apparently-To: forgery checks, regex support, POP/IMAP
  9.         sniffing, mail.local-style locking, etc.
  10.   
  11.   Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  12.   All rights reserved, all wrongs reversed.
  13.  
  14.   Redistribution and use in source and binary forms, with or without
  15.   modification, are permitted provided that the following conditions
  16.   are met:
  17.  
  18.   1. Redistributions of source code must retain the above copyright
  19.      notice, this list of conditions and the following disclaimer.
  20.   2. Redistributions in binary form must reproduce the above copyright
  21.      notice, this list of conditions and the following disclaimer in the
  22.      documentation and/or other materials provided with the distribution.
  23.   3. The name of author may not be used to endorse or promote products
  24.      derived from this software without specific prior written permission.
  25.  
  26.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  27.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  28.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  29.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  32.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  35.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  
  37.   $Id: mailsnarf.c,v 1.25 2000/05/16 18:48:01 dugsong Exp $ */
  38.  
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <netinet/in_systm.h>
  43. #include <netinet/ip.h>
  44. #include <arpa/inet.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. #include <string.h>
  49. #include <time.h>
  50. #include <nids.h>
  51.  
  52. #include "version.h"
  53.  
  54. /* bogus SMTP state machine */
  55. enum {
  56.   S_NONE = 0,
  57.   S_HELO,
  58.   S_MAIL,
  59.   S_RCPT,
  60.   S_DATA
  61. };
  62.  
  63. struct msg_info {
  64.   int smtp_state;
  65.   char *from;
  66.   char *to;
  67. };
  68.  
  69. void
  70. usage(void)
  71. {
  72.   fprintf(stderr, "Usage: mailsnarf [-i interface]\n");
  73.   exit(1);
  74. }
  75.  
  76. /* Locate substring in a binary string. */
  77. u_char *
  78. bufbuf(u_char *big, int blen, u_char *little, int llen)
  79. {
  80.   u_char *p;
  81.  
  82.   for (p = big; p <= big + blen - llen; p++) {
  83.     if (memcmp(p, little, llen) == 0)
  84.       return (p);
  85.   }
  86.   return (NULL);
  87. }
  88.   
  89. char *
  90. grep_mail_address(char *buf)
  91. {
  92.   char *p, *q;
  93.  
  94.   if ((p = strchr(buf, '<')) != NULL) {
  95.     p++;
  96.     if ((q = strchr(p, '>')) != NULL)
  97.       *q = '\0';
  98.     if (strlen(p) > 0)
  99.       return (strdup(p));
  100.   }
  101.   return (NULL);
  102. }
  103.  
  104. int
  105. process_smtp_msg(struct msg_info *msg, char *buf, int len)
  106. {
  107.   char *s, *e;
  108.   int i, discard = 0;
  109.   time_t t;
  110.   
  111.   if (msg->smtp_state != S_DATA) {
  112.     /* Process SMTP commands. */
  113.     for (s = buf;  (e = bufbuf(s, len, "\r\n", 2)) != NULL; s = e + 2) {
  114.       i = (e + 2) - s;
  115.       discard += i; len -= i;
  116.       *e = '\0';
  117.       
  118.       if (strncasecmp(s, "RSET", 4) == 0) {
  119.     msg->smtp_state = S_HELO;
  120.       }
  121.       else if (msg->smtp_state == S_NONE &&
  122.            (strncasecmp(s, "HELO", 4) == 0 ||
  123.         strncasecmp(s, "EHLO", 4) == 0)) {
  124.     msg->smtp_state = S_HELO;
  125.       }
  126.       else if (msg->smtp_state == S_HELO &&
  127.            (strncasecmp(s, "MAIL ", 5) == 0 ||
  128.         strncasecmp(s, "SEND ", 5) == 0 ||
  129.         strncasecmp(s, "SAML ", 5) == 0)) {
  130.     msg->from = grep_mail_address(s);
  131.     msg->smtp_state = S_MAIL;
  132.       }
  133.       else if (msg->smtp_state == S_MAIL &&
  134.            strncasecmp(s, "RCPT ", 5) == 0) {
  135.     msg->to = grep_mail_address(s);
  136.     msg->smtp_state = S_RCPT;
  137.       }
  138.       else if (msg->smtp_state == S_RCPT &&
  139.            strncasecmp(s, "DATA", 4) == 0) {
  140.     msg->smtp_state = S_DATA;
  141.     break;
  142.       }
  143.     }
  144.     buf = s;
  145.   }
  146.   /* Save until we get entire message. */
  147.   if (msg->smtp_state == S_DATA &&
  148.       (e = bufbuf(buf, len, "\r\n.\r\n", 5)) != NULL) {
  149.     i = (e + 5) - buf;
  150.     discard += i; len -= i;
  151.     *e = '\0';
  152.     
  153.     /* Print message all at once. */
  154.     t = time(NULL);
  155.     printf("From %s %s", msg->from ? msg->from : "dsniff", ctime(&t));
  156.  
  157.     for (s = strtok(buf, "\n"); s != NULL; s = strtok(NULL, "\n")) {
  158.       if (strncmp(s, "From ", 5) == 0)
  159.     putchar('>');
  160.       for (; *s != '\r' && *s != '\0'; s++)
  161.     putchar(*s);
  162.       putchar('\n');
  163.     }
  164.     putchar('\n');
  165.     fflush(stdout);
  166.     
  167.     if (msg->to) {
  168.       free(msg->to); msg->to = NULL;
  169.     }
  170.     if (msg->from) {
  171.       free(msg->from); msg->from = NULL;
  172.     }
  173.   }
  174.   return (discard);
  175. }
  176.  
  177.   
  178. /* XXX - Minimal SMTP FSM. We don't even consider server responses. */
  179. void
  180. sniff_smtp_client(struct tcp_stream *ts, struct msg_info **msg_save)
  181. {
  182.   struct msg_info *msg;
  183.   int i;
  184.  
  185.   /* Only handle SMTP client traffic. */
  186.   if (ts->addr.dest != 25)
  187.     return;
  188.  
  189.   switch (ts->nids_state) {
  190.   case NIDS_JUST_EST:
  191.     /* Collect data. */
  192.     ts->server.collect = 1;
  193.     if ((msg = malloc(sizeof(*msg))) == NULL)
  194.       nids_params.no_mem("sniff_smtp_client");
  195.     
  196.     msg->smtp_state = S_NONE;
  197.     msg->to = msg->from = NULL;
  198.     *msg_save = msg;
  199.     break;
  200.     
  201.   case NIDS_DATA:
  202.     msg = *msg_save;
  203.     if (ts->server.count_new != 0) {
  204.       i = process_smtp_msg(msg, ts->server.data,
  205.                ts->server.count - ts->server.offset);
  206.       nids_discard(ts, i);
  207.     }
  208.     break;
  209.     
  210.   default:
  211.     msg = *msg_save;
  212.     if (ts->server.count != 0) {
  213.       process_smtp_msg(msg, ts->server.data,
  214.                ts->server.count - ts->server.offset);
  215.     }
  216.     if (msg->to) free(msg->to);
  217.     if (msg->from) free(msg->from);
  218.     free(msg);
  219.     break;
  220.   }
  221. }
  222.  
  223. void
  224. null_syslog(int type, int errnum, struct ip *iph, void *data)
  225. {
  226. }
  227.  
  228. int
  229. main(int argc, char *argv[])
  230. {
  231.   int c;
  232.   
  233.   while ((c = getopt(argc, argv, "i:h?V")) != -1) {
  234.     switch (c) {
  235.     case 'i':
  236.       nids_params.device = optarg;
  237.       break;
  238.     case 'V':
  239.       fprintf(stderr, "Version: %s\n", VERSION);
  240.       usage();
  241.       break;
  242.     default:
  243.       usage();
  244.     }
  245.   }
  246.   argc -= optind;
  247.   argv += optind;
  248.  
  249.   if (argc != 0)
  250.     usage();
  251.  
  252.   nids_params.scan_num_hosts = 0;
  253.   nids_params.syslog = null_syslog;
  254.   
  255.   if (!nids_init()) {
  256.     fprintf (stderr, "%s\n", nids_errbuf);
  257.     exit(1);
  258.   }
  259.   nids_register_tcp(sniff_smtp_client);
  260.  
  261.   nids_run();
  262.  
  263.   /* NOTREACHED */
  264.   
  265.   exit(0);
  266. }
  267.  
  268. /* 5000. */
  269.